<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/6
 * Time: 22:11
 */
use Think\Upload,Think\Image;
//设置白名单 防止XSS攻击
function removeXSS($data){
    require_once CORE_PATH . 'HtmlPurifier/HTMLPurifier.auto.php';
    $_clean_xss_config = HTMLPurifier_Config::createDefault();
    $_clean_xss_config->set('Core.Encoding', 'UTF-8');
    $_clean_xss_config->set('HTML.Allowed','div,b,strong,i,em,a[href|title],ul,ol,li,p[style],br,span[style],img[width|height|alt|src]');
    $_clean_xss_config->set('CSS.AllowedProperties', 'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align');
    $_clean_xss_config->set('HTML.TargetBlank', TRUE);
    $_clean_xss_obj = new HTMLPurifier($_clean_xss_config);
    return $_clean_xss_obj->purify($data);
}

//显示图片
function showImage($url,$width = " ",$height = " "){
    if($width){
    $width = "width = '$width'" ;
    }
    if($height){
        $height = "height = '$height'" ;
    }
    return " <img $width $height src='http://www.shop.com/Public/Uploads/$url'/>";
}
//删除图片
function deleteImage($image = array()){
    $savePath = C('IMAGE_CONFIG');
    foreach($image as $v){
        unlink($savePath['rootPath'] .$v);
    }
}

//上传图片
function Upload($logo,$savePath,$thumb=array()){
    if(isset($_FILES[$logo]) && $_FILES[$logo]['error'] == 0){
        $ic =C('IMAGE_CONFIG');
        $upload = new Upload(
            array(
                'rootPath' => $ic['rootPath'],
                'maxSize'  => $ic['maxSize'],
                'exts'     => $ic['exts']
                )
            ); //实例化上传类
        $upload->savePath =$savePath ."/";//设置附件上传子目录
        //上传文件
        $info = $upload->upload(array($logo => $_FILES[$logo]));
        if(!$info){
            //控制器调用$model->getError();
            return array(
                'error' => $upload->getError(),
                'ok'    => 0
            );
        }else {
            $ret['ok']=1;
            $ret['images'][0] =$logoPath = $info[$logo]['savepath'] . $info[$logo]['savename'];
            if($thumb){
                $image = new Image();
                foreach ($thumb as $k => $v){
                    //生成缩略图
                    $ret['images'][$k+1] =$info[$logo]['savepath'] .'thumb_' . $k .'_'. $info[$logo]['savename'];
                    $image->open($ic['rootPath'] . $logoPath);
                    $image->thumb($v[0],$v[1])->save($ic['rootPath'] . $ret['images'][$k+1]);
                }
            }
            return $ret;
        }
    }
}

function buildSelect($tableName,$selectName,$valueFileName,$textFileName,$selectedValue=""){
        $mode = D($tableName);
        $data = $mode->field("$valueFileName,$textFileName")->select();
        $select ="<select name='$selectName'><option value =''>请选择</option>";
        foreach ($data as $k => $v){
        $value = $v[$valueFileName];
        $text  = $v[$textFileName];
        if($selectedValue && $selectedValue == $value)
            $selected = 'selected = "selected"';
        else
            $selected = '';
            $select .= '<option '. $selected . 'value="' . $value .'">' .$text .'</option>';
    }
        $select .= '</select>';
        echo  $select;
}
function makeAlipayBtn($orderId,$btnName='支付宝支付'){
    return require ('./alipay/alipayapi.php');
}
//从当前URL 中去掉某个参数之后的URL
function filterUrl($param){
    //先取出当前的URL地址
    $url = $_SERVER['PHP_SELF'];
    //正则去掉某个参数
    $re ="/\/$param\/[^\/]+/";
    return preg_replace($re,'',$url);
}